package org.neframework.webtest.action.article;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.neframework.webtest.base.BaseController;
import org.neframework.webtest.model.t_s_article;
import org.neframework.webtest.service.ArticleService;

import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
@RequestMapping("/cp/article")
public class ArticleController extends BaseController {

	private static final ArticleService articleService = new ArticleService();

	@RequestMapping("/list")
	public String list(String cate) {

		req.setAttribute("cate", cate);
		return "/jsp/article/list.jsp";
	}

	@RequestMapping("/list_data")
	@ResponseBody
	public void list_data(String srh_name, String srh_cate) {

		List<Map<String, Object>> list = articleService.selectArticleList(
				srh_name, srh_cate, null, sort_params, page);

		result.put("list", list);
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(t_s_article art) throws IOException {
		oc.saveObj(art);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String kid) {
		t_s_article art = (t_s_article) oc.findObjById(kid, t_s_article.class);

		Map<String, Object> map1 = JsonTools.objToMap("art", art);

		return JsonTools.toJson(map1);
	}

	@RequestMapping("/edit")
	@ResponseBody
	public void edit(t_s_article art) {
		t_s_article artDb = (t_s_article) oc.findObjById(art.getKid(),
				t_s_article.class);
		artDb.setTitle(art.getTitle());
		artDb.setBank_id(art.getBank_id());
		artDb.setContent(art.getContent());
		oc.updateObj(artDb);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(String kid) {
		oc.delObjById(kid, t_s_article.class);

		this.setTipMsg(true, "删除成功!", Tip.Type.success);
	}

}
